package com.argrace.smart.glide;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.graphics.drawable.RoundedBitmapDrawable;
import android.support.v4.graphics.drawable.RoundedBitmapDrawableFactory;
import android.widget.ImageView;

import com.argrace.smart.R;
import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.BitmapTransformation;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.BitmapImageViewTarget;


public class GliderImageLoaderUtil {
//    .placeholder(R.drawable.bqmm_emoji_loading)
//    .error(R.drawable.loading)

    private static RequestOptions options = new RequestOptions()
//            .centerCrop()
            .fitCenter()
            .placeholder(R.color.img_default_color)
            .error(R.color.img_default_color)
            .priority(Priority.HIGH)
            .diskCacheStrategy(DiskCacheStrategy.ALL);


    /**
     * 加载网络图片或本地文件夹图片
     */
    public static void load(Context context, String imgUrl, ImageView imgView) {
        if (isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {

            if (context != null) {
                Glide.with(context).load(imgUrl).transition(new DrawableTransitionOptions().crossFade(1500)).apply(options)
                        .into(imgView);
            }

        } else {

            if (context != null) {
                Glide.with(context).clear(imgView);
                //当资源为空时候，设置默认图片
                imgView.setImageResource(R.color.img_default_color);
            }

        }
    }


    /**
     * 加载网络图片或本地文件夹图片
     */
    public static void loadFilter(Context context, String imgUrl, ImageView imgView) {
//        if (isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {
//
//            if (context != null) {
//                Glide.with(context).load(imgUrl).apply(options)
//                        .into(imgView);
//            }
//
//        } else {
//            Glide.with(context).clear(imgView);
//            //当资源为空时候，设置默认图片
//            imgView.setImageResource(    R.color.img_default_color);
//        }


        if (context != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {

            RequestBuilder<Bitmap> bitmapRequestBuilder = Glide.with(context)
                    .asBitmap()//指定Bitmap类型的RequestBuilder
                    .load(imgUrl)//网络URL
                    .apply(options);//当url为空时，显示图片

            RequestOptions requestOptions = new RequestOptions();
            //在RequestOptions中使用Transformations

            requestOptions.transform(new ColorFilterTransformation(context, 0x50000000));

            //RequestBuilder<Bitmap> 中添加RequestOptions
            bitmapRequestBuilder.apply(requestOptions).into(imgView);

        } else {
            if (context != null) {
                Glide.with(context).clear(imgView);
                //当资源为空时候，设置默认图片
                imgView.setImageResource(R.color.img_default_color);
            }
        }


    }

    /**
     * 加载网络图片或本地文件夹图片---圆角
     */
    public static void load(Context context, String imgUrl, ImageView imgView, GlideRoundedCornersTransform.CornerType type, int radio) {
        if (isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {

            RequestOptions requestOptions = new RequestOptions();
            requestOptions.optionalTransform(new GlideRoundedCornersTransform(radio,
                    type)).placeholder(R.color.img_default_color);
            if (context != null) {
                Glide.with(context).load(imgUrl)
//                        .transition(new DrawableTransitionOptions().crossFade(5500))
                        .apply(requestOptions)
                        .into(imgView);
            }

        } else {
            if (context != null) {
                Glide.with(context).clear(imgView);
                //当资源为空时候，设置默认图片
                imgView.setImageResource(R.color.img_default_color);
            }
        }
    }

//    /**
//     * 加载网络图片或本地文件夹图片---圆角
//     */
//    public static void loadCenterCrop(Context context, String imgUrl, ImageView imgView, GlideRoundedCornersTransform.CornerType type, int radio) {
//        if (isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {
//
//            RequestOptions requestOptions = new RequestOptions();
//            requestOptions.optionalTransform(new GlideRoundedCornersTransform(radio,
//                    type)).placeholder(R.color.img_default_color);
//            if (context != null) {
//                Glide.with(context).load(imgUrl)
////                        .transition(new DrawableTransitionOptions().crossFade(5500))
//                        .apply(requestOptions)
//                        .into(imgView);
//            }
//
//        } else {
//            if(context!=null){
//                Glide.with(context).clear(imgView);
//                //当资源为空时候，设置默认图片
//                imgView.setImageResource(R.color.img_default_color);
//            }
//        }
//    }

    /**
     * 加载网络图片或本地文件夹图片
     */
    public static void load(Context context, Uri rui, ImageView imgView) {
        if (rui != null) {
            if (context != null) {
                Glide.with(context).load(rui).transition(new DrawableTransitionOptions().crossFade(1500)).apply(new RequestOptions()
                        .centerCrop()
//                        .placeholder(R.color.grey)
                        .error(R.color.img_default_color)
                        .priority(Priority.HIGH)
                        .diskCacheStrategy(DiskCacheStrategy.NONE))
                        .into(imgView);
            }

        } else {
            if (context != null) {
                Glide.with(context).clear(imgView);
                //当资源为空时候，设置默认图片
                imgView.setImageResource(R.color.img_default_color);
            }
        }
    }


    public static void loadRound(Context context, String imgUrl,
                                 ImageView imgView) {
        if (context != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {

            RequestBuilder<Bitmap> bitmapRequestBuilder = Glide.with(context)
                    .asBitmap()//指定Bitmap类型的RequestBuilder
                    .load(imgUrl)//网络URL
                    .apply(options);//当url为空时，显示图片

            RequestOptions requestOptions = new RequestOptions();
            //在RequestOptions中使用Transformations
            requestOptions.transform(new GlideRoundTransform(context));

            //RequestBuilder<Bitmap> 中添加RequestOptions
            bitmapRequestBuilder.apply(requestOptions).into(imgView);

        } else {
            if (context != null) {
                Glide.with(context).clear(imgView);
                //当资源为空时候，设置默认图片
                imgView.setImageResource(R.color.img_default_color);
            }
        }


    }

    public static void loadRound(Context context, String imgUrl,
                                 ImageView imgView, int errorImg) {
        if (context != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {
            RequestBuilder<Bitmap> bitmapRequestBuilder = Glide.with(context)
                    .asBitmap()//指定Bitmap类型的RequestBuilder
                    .load(imgUrl)//网络URL

                    .apply(new RequestOptions()
                            .centerCrop()
                            .placeholder(R.color.img_default_color)
                            .error(errorImg)
                            .priority(Priority.HIGH)
                            .diskCacheStrategy(DiskCacheStrategy.ALL));//当url为空时，显示图片

            RequestOptions requestOptions = new RequestOptions();
            //在RequestOptions中使用Transformations
            requestOptions.transform(new GlideRoundTransform(context));

            //RequestBuilder<Bitmap> 中添加RequestOptions
            bitmapRequestBuilder.apply(requestOptions).into(imgView);
        } else {
            if (context != null) {
                Glide.with(context).clear(imgView);
                //当资源为空时候，设置默认图片
                imgView.setImageResource(errorImg);
            }

        }


    }

    public static void loadRoundMp(Context context, String imgUrl,
                                   ImageView imgView, int errorImg) {
        if (context != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {
            RequestBuilder<Bitmap> bitmapRequestBuilder = Glide.with(context)
                    .asBitmap()//指定Bitmap类型的RequestBuilder
                    .load(imgUrl)//网络URL

                    .apply(new RequestOptions()
                            .centerCrop()
                            .placeholder(R.mipmap.ic_launcher)
                            .error(errorImg)
                            .priority(Priority.HIGH)
                            .diskCacheStrategy(DiskCacheStrategy.ALL));//当url为空时，显示图片

            RequestOptions requestOptions = new RequestOptions();
            //在RequestOptions中使用Transformations
            requestOptions.transform(new GlideRoundTransform(context));

            //RequestBuilder<Bitmap> 中添加RequestOptions
            bitmapRequestBuilder.apply(requestOptions).into(imgView);
        } else {
            if (context != null) {
                Glide.with(context).clear(imgView);
                //当资源为空时候，设置默认图片
                imgView.setImageResource(errorImg);
            }
        }


    }


    public static void loadRoundCustom(Context context, String imgUrl,
                                       ImageView imgView, int placeholderImg, int errorImg) {
        if (context != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {

            RequestBuilder<Bitmap> bitmapRequestBuilder = Glide.with(context)
                    .asBitmap()//指定Bitmap类型的RequestBuilder
                    .load(imgUrl)//网络URL
                    .apply(new RequestOptions()
                            .centerCrop()
                            .placeholder(placeholderImg)
                            .error(errorImg)
                            .priority(Priority.HIGH)
                            .diskCacheStrategy(DiskCacheStrategy.ALL));//当url为空时，显示图片

            RequestOptions requestOptions = new RequestOptions();
            //在RequestOptions中使用Transformations
            requestOptions.transform(new GlideRoundTransform(context));

            //RequestBuilder<Bitmap> 中添加RequestOptions
            bitmapRequestBuilder.apply(requestOptions).into(imgView);
        } else {
            if (context != null) {
                Glide.with(context).clear(imgView);
                //当资源为空时候，设置默认图片
                imgView.setImageResource(errorImg);
            }
        }


    }


    public static void loadNativRound(Context context, int imgUrl,
                                      ImageView imgView) {
        if (context != null) {
            RequestBuilder<Bitmap> bitmapRequestBuilder = Glide.with(context)
                    .asBitmap()//指定Bitmap类型的RequestBuilder
                    .load(imgUrl)//网络URL
                    .apply(options);//当url为空时，显示图片

            RequestOptions requestOptions = new RequestOptions();
            //在RequestOptions中使用Transformations
            requestOptions.transform(new GlideRoundTransform(context));

            //RequestBuilder<Bitmap> 中添加RequestOptions
            bitmapRequestBuilder.apply(requestOptions).into(imgView);
        } else {
            imgView.setImageResource(imgUrl);
        }

    }


    public static void loadRoundMore(final Context context, String imgUrl,
                                     final ImageView imgView) {
        if (context != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {

            RequestBuilder<Bitmap> bitmapRequestBuilder = Glide.with(context)
                    .asBitmap()//指定Bitmap类型的RequestBuilder
                    .load(imgUrl)//网络URL
                    .apply(options);//当url为空时，显示图片

            RequestOptions requestOptions = new RequestOptions();
            //在RequestOptions中使用Transformations
            requestOptions.transform(new GlideRoundTransform(context));

            //RequestBuilder<Bitmap> 中添加RequestOptions
            bitmapRequestBuilder.apply(requestOptions)

//                    .into(imgView);
//
//
//
//            Glide.with(context).load(imgUrl).asBitmap().placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color).centerCrop()

                    .into(new BitmapImageViewTarget(imgView) {
                        @Override
                        protected void setResource(Bitmap resource) {
                            RoundedBitmapDrawable circularBitmapDrawable =
                                    RoundedBitmapDrawableFactory.create(context.getResources(), resource);
                            circularBitmapDrawable.setCircular(true);
                            imgView.setImageDrawable(circularBitmapDrawable);
                        }
                    });
        } else {

            if (context != null) {

                Glide.with(context).clear(imgView);
                //当资源为空时候，设置默认图片
                imgView.setImageResource(R.color.img_default_color);
            }
        }
    }


    public static void load(Context context, String imgUrl, ImageView imgView,
                            BitmapTransformation transformations) {
        if (context != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {
            RequestBuilder<Bitmap> bitmapRequestBuilder = Glide.with(context)
                    .asBitmap()//指定Bitmap类型的RequestBuilder
                    .load(imgUrl)//网络URL
                    .apply(options);//当url为空时，显示图片

            RequestOptions requestOptions = new RequestOptions();
            //在RequestOptions中使用Transformations
            requestOptions.transform(new GlideRoundTransform(context));

            //RequestBuilder<Bitmap> 中添加RequestOptions
            bitmapRequestBuilder.apply(requestOptions).into(imgView);
        } else {

            if (context != null) {

                Glide.with(context).clear(imgView);
                //当资源为空时候，设置默认图片
                imgView.setImageResource(R.color.img_default_color);
            }
        }

    }

    public static void load(Activity activity, String imgUrl, ImageView imgView) {
        if (activity != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {
//            Glide.with(activity).load(imgUrl).placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
////                .error(R.mipmap.ic_carmall)
//                    .into(imgView);
            Glide.with(activity).load(imgUrl).transition(new DrawableTransitionOptions().crossFade(1500)).apply(options)
                    .into(imgView);

        } else {
            Glide.with(activity).clear(imgView);
            //当资源为空时候，设置默认图片
            imgView.setImageResource(R.color.img_default_color);
        }
    }

    public static void loadNoDrawableTransition(Activity activity, String imgUrl, ImageView imgView) {
        if (activity != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {
//            Glide.with(activity).load(imgUrl).placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL)
////                .error(R.mipmap.ic_carmall)
//                    .into(imgView);
            Glide.with(activity).load(imgUrl).apply(new RequestOptions()
                    .centerCrop()
                    .placeholder(R.mipmap.ic_launcher)
                    .error(R.mipmap.ic_launcher)
                    .priority(Priority.HIGH)
                    .diskCacheStrategy(DiskCacheStrategy.ALL))
                    .into(imgView);

        } else {
            Glide.with(activity).clear(imgView);
            //当资源为空时候，设置默认图片
            imgView.setImageResource(R.color.img_default_color);
        }
    }


    public static void load(Activity activity, String imgUrl,
                            ImageView imgView, BitmapTransformation transformations) {

        if (activity != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {
            RequestBuilder<Bitmap> bitmapRequestBuilder = Glide.with(activity)
                    .asBitmap()//指定Bitmap类型的RequestBuilder
                    .load(imgUrl)//网络URL
                    .apply(options);//当url为空时，显示图片

            RequestOptions requestOptions = new RequestOptions();
            //在RequestOptions中使用Transformations
            requestOptions.transform(transformations);

            //RequestBuilder<Bitmap> 中添加RequestOptions
            bitmapRequestBuilder.apply(requestOptions).into(imgView);
        } else {
            Glide.with(activity).clear(imgView);
            //当资源为空时候，设置默认图片
            imgView.setImageResource(R.color.img_default_color);
        }
    }

    public static void load(android.support.v4.app.Fragment fragment,
                            String imgUrl, ImageView imgView) {

        if (fragment != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {


//            Glide.with(fragment).load(imgUrl).placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL).into(imgView);


            Glide.with(fragment).load(imgUrl).transition(new DrawableTransitionOptions().crossFade(1500)).apply(options)
                    .into(imgView);
        } else {
            Glide.with(fragment).clear(imgView);
            //当资源为空时候，设置默认图片
            imgView.setImageResource(R.color.img_default_color);
        }
    }

//    public static void load(android.support.v4.app.Fragment fragment,
//                            String imgUrl, ImageView imgView,
//                            BitmapTransformation transformations) {
//        if (fragment != null) {
//            RequestBuilder<Bitmap> bitmapRequestBuilder= Glide.with(fragment)
//                    .asBitmap()//指定Bitmap类型的RequestBuilder
//                    .load(imgUrl)//网络URL
//                    .apply(options);//当url为空时，显示图片
//
//            RequestOptions requestOptions=new RequestOptions();
//            //在RequestOptions中使用Transformations
//            requestOptions.transform(new GlideRoundTransform(fragment));
//
//            //RequestBuilder<Bitmap> 中添加RequestOptions
//            bitmapRequestBuilder.apply(requestOptions).into(imgView);
//        }
//    }

    public static void load(android.app.Fragment fragment, String imgUrl,
                            ImageView imgView) {
        if (fragment != null && isStringNotEmpty(imgUrl) && imgUrl.length() > 0) {
//            Glide.with(fragment).load(imgUrl).placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL).into(imgView);

            Glide.with(fragment).load(imgUrl).transition(new DrawableTransitionOptions().crossFade(1500)).apply(options)
                    .into(imgView);
        } else {
            Glide.with(fragment).clear(imgView);
            //当资源为空时候，设置默认图片
            imgView.setImageResource(R.color.img_default_color);
        }
    }

//    public static void load(android.app.Fragment fragment, String imgUrl,
//                            ImageView imgView, BitmapTransformation transformations) {
//        if (fragment != null) {
//            Glide.with(fragment).load(imgUrl).apply(new RequestOptions()
//                    .centerCrop()
//                    .placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color)
//                    .priority(Priority.HIGH)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL))
//                    .transform(transformations).into(imgView);
//        }
//    }

    /**
     * 加载本地drawable图片（包括gif）
     */
    public static void load(Context context, int drawableId, ImageView imgView) {
        if (context != null) {
//            Glide.with(context).load(drawableId).placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL).into(imgView);

            Glide.with(context).load(drawableId).transition(new DrawableTransitionOptions().crossFade(1500)).apply(options)
                    .into(imgView);
        }
    }

//    /**
//     * 快速加载gif图
//     */
//    public static void loadGif(Context context, int drawableId,
//                               ImageView imgView) {
//        if (context != null) {
//            Glide.with(context).load(drawableId).asGif().placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color)
//                    .diskCacheStrategy(DiskCacheStrategy.SOURCE).into(imgView);
//        }
//    }

//    public static void load(Context context, int drawableId, ImageView imgView,
//                            BitmapTransformation transformations) {
//        if (context != null) {
//            Glide.with(context).load(drawableId).apply(new RequestOptions()
//                    .centerCrop()
//                    .placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color)
//                    .priority(Priority.HIGH)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL))
//                    .transform(transformations).into(imgView);
//        }
//    }

    public static void load(Activity activity, int drawableId, ImageView imgView) {
        if (activity != null) {
            Glide.with(activity).load(drawableId).transition(new DrawableTransitionOptions().crossFade(1500))
                    .apply(options).into(imgView);
        }
    }

//    public static void load(Activity activity, int drawableId,
//                            ImageView imgView, BitmapTransformation transformations) {
//        if (activity != null) {
//            Glide.with(activity).load(drawableId).apply(new RequestOptions()
//                    .centerCrop()
//                    .placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color)
//                    .priority(Priority.HIGH)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL))
//                    .transform(transformations).into(imgView);
//        }
//    }

    public static void load(android.support.v4.app.Fragment fragment,
                            int drawableId, ImageView imgView) {
        if (fragment != null) {
            Glide.with(fragment).load(drawableId).transition(new DrawableTransitionOptions().crossFade(1500)).apply(options).into(imgView);
        }
    }

    public static void load(android.support.v4.app.Fragment fragment,
                            int drawableId, ImageView imgView,
                            BitmapTransformation transformations) {
        if (fragment != null) {
//            Glide.with(fragment).load(drawableId).apply(new RequestOptions()
//                    .centerCrop()
//                    .placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color)
//                    .priority(Priority.HIGH)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL))
//                    .transform(transformations).into(imgView);

            RequestBuilder<Bitmap> bitmapRequestBuilder = Glide.with(fragment)
                    .asBitmap()//指定Bitmap类型的RequestBuilder
                    .load(drawableId)//网络URL
                    .apply(options);//当url为空时，显示图片

            RequestOptions requestOptions = new RequestOptions();
            //在RequestOptions中使用Transformations
            requestOptions.transform(transformations);

            //RequestBuilder<Bitmap> 中添加RequestOptions
            bitmapRequestBuilder.apply(requestOptions).into(imgView);
        }
    }

    public static void load(android.app.Fragment fragment, int drawableId,
                            ImageView imgView) {
        if (fragment != null) {
            Glide.with(fragment).load(drawableId).transition(new DrawableTransitionOptions().crossFade(1500)).apply(options).into(imgView);
        }
    }

    public static void load(android.app.Fragment fragment, int drawableId,
                            ImageView imgView, BitmapTransformation transformations) {
        if (fragment != null) {
//            Glide.with(fragment).load(drawableId).apply(new RequestOptions()
//                    .centerCrop()
//                    .placeholder(    R.color.img_default_color)
//                    .error(    R.color.img_default_color)
//                    .priority(Priority.HIGH)
//                    .diskCacheStrategy(DiskCacheStrategy.ALL))
//                    .transform(transformations).into(imgView);

            RequestBuilder<Bitmap> bitmapRequestBuilder = Glide.with(fragment)
                    .asBitmap()//指定Bitmap类型的RequestBuilder
                    .load(drawableId)//网络URL
                    .apply(options);//当url为空时，显示图片

            RequestOptions requestOptions = new RequestOptions();
            //在RequestOptions中使用Transformations
            requestOptions.transform(transformations);

            //RequestBuilder<Bitmap> 中添加RequestOptions
            bitmapRequestBuilder.apply(requestOptions).into(imgView);
        }
    }

    public static boolean isStringNotEmpty(String value) {
        return value != null && !"".equals(value) && !"null".equals(value);
    }
}
